package com.dzx.swing;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.SecureRandom;
import java.util.Random;

/**
 * @author dzx
 * @ClassName:
 * @Description:
 * @date 2019年07月26日 16:11:24
 */
public class JProgressBarDemo implements ActionListener, ChangeListener {

    JFrame frame = null;
    JProgressBar progressbar;
    JLabel label;
    Timer timer;
    JButton b;
    JButton c;

    public JProgressBarDemo() {
        frame = new JFrame("软件安装");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = frame.getContentPane();
        label = new JLabel("", JLabel.CENTER);
        progressbar = new JProgressBar();
        progressbar.setOrientation(JProgressBar.HORIZONTAL);
        progressbar.setMinimum(0);
        progressbar.setMaximum(100);
        progressbar.setValue(0);

        progressbar.setStringPainted(true);
        progressbar.addChangeListener(this);
        progressbar.setPreferredSize(new Dimension(300, 20));
        progressbar.setBorderPainted(true);

        progressbar.setBackground(Color.pink);

        //添加启动按钮
        JPanel jPanel = new JPanel();
        b = new JButton("安装");
        b.setForeground(Color.BLUE);
        //添加事件监听器
        b.addActionListener(this);
        jPanel.add(b);

        c = new JButton("暂停");
        c.setForeground(Color.BLUE);
        c.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() instanceof JButton) {
                    JButton source = (JButton) e.getSource();
                    if (source.getText().equals("暂停")) {
                        timer.stop();
                        source.setText("继续");
                    } else if (source.getText().equals("继续")) {
                        timer.start();
                        source.setText("暂停");
                    }
                }
            }
        });

        jPanel.add(c);
        jPanel.setSize(500, 150);

        timer = new Timer(800, this);
        contentPane.add(jPanel, BorderLayout.NORTH);
        contentPane.add(progressbar, BorderLayout.CENTER);
        contentPane.add(label, BorderLayout.SOUTH);
        frame.setSize(600, 200);
       // frame.pack();
        frame.setVisible(true);


    }


    public static void main(String[] agrs) {
        new JProgressBarDemo();    //创建一个实例化对象
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b) {
            timer.start();
        }
        if (e.getSource() == timer) {
            int value = progressbar.getValue();
            if (value < 100) {
                value += new Random().nextInt(5);
                progressbar.setValue(value);
            } else {
                timer.stop();
                frame.dispose();
            }
        }
    }

    @Override
    public void stateChanged(ChangeEvent e) {

        String[] name = new String[]{"张三", "李四", "小美", "小明"};
        String[] event = new String[]{"边吃零食边看电视~", "愉快的享受着双人国外游~",
                "和小伙伴们开黑打游戏~", "化妆的路上越走越远~"};

        SecureRandom random = new SecureRandom();
        //判断事件源是否是进度体
        if (e.getSource() == progressbar) {
            label.setText(name[random.nextInt(4)] + "正在" + event[random.nextInt(4)] + ",目前以完成安装进度:" + progressbar.getValue());
            label.setForeground(Color.BLUE);
        }
        //如果是进度条，则修改提示文字的前景色为蓝色 并且提示进度
    }
}
